<style>

    .layui-form-label {
        width: 110px;
    }

</style>
{extend name='public/window' /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form">
                {if isset($info.id)}
                <input type="hidden" name="id" value="{$info.id}">
                {/if}
                <input type="hidden" name="__token__" id="token" value="{:token()}"/>
                <!--                <div class="layui-form-item">-->
                <!--                    <label class="layui-form-label">商户类型</label>-->
                <!--                    <div class="layui-input-inline">-->
                <!--                        <select name="merchant_type"  id="type_select"   lay-filter="merchant_type">-->
                <!--                            <option value="1">常驻</option>-->
                <!--                            <option value="2">临户</option>-->
                <!--                        </select>-->
                <!--                    </div>-->
                <!--                </div>-->
                <div class="layui-form-item merch">
                    <label class="layui-form-label">商户</label>
                    <div class="layui-input-inline" style="width: 410px;">
                        <select name="merchant_id" lay-search="" id="merchant_id"  lay-filter="merchant_id">
                            <option value="" >请选择</option>
                            {volist name="merchant_list" id="t"}
                            {if empty($t.name)||$t.name=='-'}
                            <option value="{$t.id}" >{$t.company} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                            {else/}
                            <option value="{$t.id}" >{$t.name} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                            {/if}
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">缴费类型</label>
                    <div class="layui-input-inline">
                        <select name="cost_type_id" id="cost_type_list" lay-search=""  lay-verify="required" lay-filter="cost_type_list"  lay-verify="required">
                            <option value="0" >请选择</option>
                            {volist name="cost_type_list" id="t"}
                            <option value="{$t.id}" >{$t.cost_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">缴费区间</label>
                    <div class="layui-inline" id="test6">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="start_time" id="start_time" class="layui-input" placeholder="开始日期">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off"  name="end_time" id="end_time" class="layui-input" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">缴费金额 *</label>
                    <div class="layui-input-block">
                        <input type="number" name="amount"  lay-verify="required" lay-filter="amount" autocomplete="off" placeholder="请输入缴费金额" class="layui-input" id="amount">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注 </label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容"  class="layui-textarea" name="remark"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin " style="width: 100%">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0px;text-align: center;">
                            <button class="layui-btn" lay-submit="" lay-filter="add">{:__('Submit')}</button>
                            <button type="reset" class="layui-btn layui-btn-primary">{:__('Reset')}</button>
                        </div>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn  layui-btn-danger layui-btn-xs" data-title="删除" lay-event="del"><i class="layui-icon "></i></a>
</script>
<script>
    layui.use(['element', 'form', 'table', 'jquery','lucky','laydate','xmSelect'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var lucky=layui.lucky;
        laydate.render({
            elem: '#start_time'
            ,type: 'date',
            trigger: 'click'
        });
        laydate.render({
            elem: '#end_time'
            ,type: 'date',
            trigger: 'click'
        });
        form.on('select(merchant_type)', function(data){
            var  type = data.value;
            if(type==1){
                $(".shinfo").css('display','none');
                $(".merch").css('display','block');
            }else{
                $(".shinfo").css('display','block');
                $(".merch").css('display','none');
            }
        });
        var costType = [];
        var student_table = table.render({
            id:'student_table',
            elem:'#student_table',
            cols: [[
                {field: 'cost_name', title: '收费名称'},
                {field: 'type', title: '收费类型', templet: function (item) {
                        var type = item.type
                        if (type == 1) {
                            return '押金'
                        } else if (type == 2) {
                            return '费用'
                        } else if (type == 3) {
                            return '证件'
                        } else {
                            return '未知类型'
                        }
                    }},
                // {title: '单价', field: 'unitprice'},
                {title: '税率', field: 'tax_rate'},
                {title: '税率金额', field: 'tax_money'},
                {title: '收入金额', field: 'money'},
                {title: '总金额', field: 'z_money'},
            ]],
            data: costType
        });
        $("#amount").on("input",function(e){
            // console.log('监听中.....')
            student_table.reload({data:[]});
            var amount = e.delegateTarget.value;
            var cost_type = $("#cost_type_list").val();
            if (!cost_type){
                return false;
            }
            var datas = {} ;
            var  number = $("#number").val();
            datas.amount      = amount ;   //  购买数量
            datas.number      = number ;   //  购买数量
            datas.cost_type   = cost_type ;   // 缴费类型
            // console.log('datas',datas)
            // costtype(datas) ;
            return false;
        });
        $("#number").on("input",function(e){
            // console.log('监听中.....')
            student_table.reload({data:[]});
            var number = e.delegateTarget.value;
            var cost_type = $("#cost_type_list").val();
            if (!cost_type){
                return false;
            }
            var datas = {} ;
            var  amount = $("#amount").val();
            // console.log(amount)
            datas.number      = number ;   //  购买数量
            datas.amount      = amount ;   //  购买数量
            datas.cost_type   = cost_type ;   // 缴费类型
            // console.log('datas',datas)
            costtype(datas) ;
            return false;
        });
        form.on('select(merchant_id)',function (data){
            console.log(data.value)
            $('#contro_no').empty();
            form.render();
            // merchant_info(data.value);

        })
        // function merchant_info(datas){
        //     $.ajax({
        //         url:"{:url('getConInfo')}",
        //         type:"post",
        //         data: {
        //             merchant_id:datas
        //         },
        //         dataType:"json",
        //         success:function(data){
        //
        //
        //             $.each(data.data['contract_info'], function(index, item) {
        //                 console.log(item)
        //                 //赋值
        //                $('#contro_no').append(new Option(item.brand, item.contract_no));
        //             });
        //
        //             form.render('select');
        //         }
        //     });
        // }
        // cost_type_list
        form.on('select(cost_type_list)', function(data){
            student_table.reload({data:[]});
            if (!data.value){
                return false
            }
            var  amount = $("#amount").val();
            var  number = $("#number").val();
            var datas = {} ;
            datas.amount      = amount ;   //  购买数量
            datas.number      = number ;   //  购买数量
            datas.cost_type   = data.value ;   // 缴费类型
            // console.log('datas',datas)
            // costtype(datas) ;
            return false;
        });

        function costtype(datas){
            $.ajax({
                url:"{:url('getCostType')}",
                type:"post",
                data: datas,
                dataType:"json",
                success:function(data){
                    var param = data.data
                    $("#amount").val(data.data.dan_money);
                    student_table.reload({data:param.data});
                }
            });
        }
        //表单赋值
        var info = {:json_encode($info)};
        form.val("form",info);
        form.on('submit(add)', function (data) {
            var datas  = layui.table.cache["student_table"]
            data.field.type = 2 // 添加type
            data.field.is_admin_create = 1 // 添加type
            data.field.pay_status = 0 // 支付状态
            data.field.datas = datas // 添加type
            lucky.FormSubmit("{:url('add')}",data.field,true);
            return false;
        });
    })
</script>

{/block}

